/**
 * Created by lixiaodi on 2019/06/17 0027.
 */
mini.parse();
var tree = mini.get("tree1");       //得到树
var grid = mini.get('datagrid1');	//得到数据表
//var searchForm = new mini.Form("searchFormBar");	//搜索栏目
var pager = grid.getBottomPager();					//分页控件
//树结构显示
$(function(){
	//请求树
	httpTree();
});

//选择节点
tree.on("nodeselect", function (e) {
    var nodes = e.node;
    if (nodes) {
        var depId = nodes.pk;
        var types = nodes.id.substring(0,2);
        httpNode(depId,types);
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});

//带有父页面传来的值
function SetData(data){
	console.log(data);
}
//保存
function SaveData(){
	var selectMenus = grid.getSelecteds();
	if(selectMenus){
		CloseWindow('ok')
	}else{
		mini.alert('请选择父级菜单');
	}
}
//给父页面传值
function GetData(){
	var selectRow = grid.getSelected();
	return selectRow;
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//请求树并给人员表赋值
function httpTree(){
	$.ajax({
        url: ajaxUrl + "/TreeController/getAllTree.do",
        type:"post",
        success: function (data) {
            tree.setData(data.data);
            treeData = data.data;
            var nodeId = treeData[0].pk;
            var types = treeData[0].id.substring(0,2);
            httpNode(nodeId,types);
        },
        error:function(data){
            mini.alert("失败");
        }
    });
}
//点击树节点请求
function httpNode(nodeId,types){
	$.ajax({
        url:ajaxUrl + "/personnerlUser/getPersonnerListBypid.do",
        type:"POST",
        data : {
            id : nodeId,
            types : types
        },
        success : function(data){
            if(data.data){
            	var personData = data.data;
            	for(var i = 0; i < personData.length; i++){
            		//日期格式化
            		if(personData[i].createDate){
            			personData[i].createDate = formatDate(personData[i].createDate);
            		}else{
            			personData[i].createDate = '';
            		}
            		//出生日期
            		if(personData[i].dateBirth){
            			personData[i].dateBirth = formatDate(personData[i].dateBirth);
            		}else{
            			personData[i].dateBirth = '';
            		}
            		//是否操作员
            		personData[i].isOperator = personData[i].isOperator == 1 ? '是' : '否';
            		//模块状态
            		if(personData[i].modularState){
            			personData[i].modularState = personData[i].modularState == 1 ? '开启' : '关闭';
            		}else{
            			personData[i].modularState = '';
            		}
            	}
                grid.setData(personData);
            }else{
                showTips(data.msg);
                grid.setData(data.data);
            }
        },
        error:function(data){
            mini.alert("失败");
        }
    });
}